<template>
	<view class="cont" v-if="isLogin">
		<navi-shell :back="true" v-bind:title="studentList.length + ' 门课程'">
		</navi-shell>
		<view class="content">
			<view class="label" @click="courseAll(1)">
				<text>所有课程</text>
			</view>
			<view class="label" @click="courseAll(2)">
				<text>申请中</text>
			</view>
			<view class="label" @click="courseAll(3)">
				<text>我的课程</text>
			</view>
			<view class="label" @click="courseAll(4)">
				<text>申请失败</text>
			</view>
		</view>
		<view class="sCoures">
			<view v-for="(slist,index) in studentList" :key="slist.id" class="sCoures-display">
				<image :src="'http://localhost:8081/'+slist.cover" mode="aspectFill" class="img-avatar"
					@click.stop="clickShowUser(slist)">
				</image>
				<text v-if="slist.status==0" class="sCoures-sq">申请中</text>
				<text class="sCoures-text">{{slist.name}}</text>
				<text v-if="slist.status==1" class="sCoures-tg">✔</text>
			</view>
		</view>
		<dialog-shell ref="shell" @sqkc='clickApplication(id)' v-bind:title="title"
			v-bind:confirmText="status==2 ? '申请课程':'确定'">

			<image :src="userAvatar" mode="aspectFill" class="teacher-avatar"></image>
			<text class="teacher-name">{{userDiscNick}}</text>
			<text class="course-disc">{{cousreDisc}}</text>
		</dialog-shell>
	</view>
</template>

<script>
	let app = getApp()
	export default {
		data() {
			return {
				isLogin: false,
				studentList: [],
				coures: [],
				users: [],
				userDiscNick: '',
				userAvatar: '',
				cousreDisc: '',
				title: '',
				status: '',
				id: '',
			}
		},
		onLoad() {
			this.courseAll(1)
		},
		onPullDownRefresh() {
			this.studentList = [],
				this.courseAll(1)
			uni.stopPullDownRefresh()
		},
		mounted() {
			this.isLogin = app.globalData.token.length > 0
			if (!this.isLogin) {
				delta = 2
				this.$refs.login.show()
			}
		},
		methods: {
			courseAll(number) {
				let url
				switch (number) {
					case 1:
						url = this.$params.host + this.$params.action_course
						break
					case 2:
						url = this.$params.host + this.$params.action_applicant_applying
						break
					case 3:
						url = this.$params.host + this.$params.action_applicant_enrolled
						break
					case 4:
						url = this.$params.host + this.$params.aticon_applicant_declined
						break
				}

				let data = {
					token: app.globalData.token,
					id: app.globalData.uid
				}
				this.$request.get(url, data, res => {
					this.slist = res.data.data
					console.log(number)
					this.studentList = res.data.data
				});
			},
			clickShowUser(slist) {
				this.title = slist.name
				this.id = slist.courseId
				this.status = slist.status
				this.userAvatar = slist.teacherAvatar
				this.userDiscNick = slist.teacherName
				this.cousreDisc = slist.desc
				this.$refs.shell.showDialog()
			},
			clickApplication(id) {
				let url = this.$params.host + this.$params.api_v1 + this.$params.applicant + this.$params.apply
				let data = {
					token: app.globalData.token,
					studentId: app.globalData.uid,
					"id": id
				}
				this.$request.post(url, data, res => {
					console.log(res)
					this.studentList.forEach(b => {
						if (b.id == id) {
							b.status = res.data.status
						}
					})
				})
			},
		}
	}
</script>

<style>
	@import url("stuclass.css");
</style>
